<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

class Other extends Controller
{
    public function Other(){
    	$user_id = Session::get('id');//当前登录用户id
    	$uid = Cookie::get('uid');

    	if($user_id){
			/*是否是好友*/
			$is_friend = Db::table('tp_friend')
						 ->where(array('user_id'=>$user_id,'friend_id'=>$uid))
						 ->whereOr("user_id = $uid and friend_id = $user_id")
						 ->count();
			
			
			/*关注*/
			$is_gz = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$uid,'isdelete'=>0))->count();
					
    	}
    	//已策划2次活动/获得15个赞
		$active_count = Db::table("tp_info")->where(array('user_id'=>$uid))->count();//活动次数
		$zan_count = Db::table("tp_info")->where(array('user_id'=>$uid))->sum('zan');//点赞次数
    	//活动发布者信息
    	$user_info = Db::table("tp_user")->where(array("id"=>$uid))->find();
    	/*活动信息*/
    	$info = Db::table("tp_info")->alias("i")
				->join("tp_user u","u.id = i.user_id","left")
				->field("i.*,u.img,u.id as uid,u.name,u.age,u.is_rz")
				->where(array("i.isdelete"=>0,'u.id'=>$uid))
				->select();
		foreach($info as $k => $v){
			if($v){
				/*$hot_info[$k]['create_time'] = date("m-d H:i",$v['create_time']);
				$hot_info[$k]['go_time'] = date("m月d日",$v['go_time']);*/
				$info[$k]['comment_count'] = Db::table('tp_comment')->where(array('active_id'=>$v['id']))->count();
			}
		}
		$this->view->assign('user_id',$user_id?$user_id:0);//当前登录用户id
		$this->view->assign('is_friend',$is_friend?$is_friend:0);//是否是好友,0不是，1是
		$this->view->assign('is_gz',$is_gz?$is_gz:0);//是否关注,0未关注，1已关注
		$this->view->assign('active_count',$active_count);//发布活动数量
		$this->view->assign('uid',$uid);//活动发布者id
		$this->view->assign('zan_count',$zan_count);//点赞数量
		$this->view->assign('user_info',$user_info);//活动发布者信息
		$this->view->assign('info',$info);//活动信息
    	return $this->view->fetch();
    }
    /*添加好友*/
    public function other_yan(){
    	$user_id = Session::get('id');//当前登录者id
//  	$user_id = $_GET['user_id'];
    	$uid = $_REQUEST['uid'];//活动发布者id
    	if($user_id != 0){
	    	$user_info = Db::table("tp_user")->where(array("id"=>$uid))->find();
	    	
	    	$info = Db::table("tp_info")
					->where(array("isdelete"=>0,'user_id'=>$uid))
					->order('create_time desc')
					->find();
	    	
	    	$is_gz = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$uid,'isdelete'=>0))->count();//1已关注，0未关注
	    	
			$this->view->assign('is_gz',$is_gz);
			$this->view->assign('user_info',$user_info);
	    	$this->view->assign('uid',$uid);
			$this->view->assign('user_id',$user_id);
			$this->view->assign('info',$info);
	    	return $this->view->fetch();
    	}
    }
    /*关注好友设置当前网址至session*/
   	public function set_other_addfriend(){
   		Session::set('url','/index/other/add_friend');
   		return 1;
  	}
    /*加好友的验证消息*/
   	public function add_friend(){
   		$timer = time();
   		$data['content'] = $_POST['content'];
   		$data['friend_id'] = $_POST['user_id'];//当前登录账号id(发送消息者)
   		$data['user_id'] = $_POST['uid'];//活动发布者id（接收信息者）
   		$data['create_time'] = $timer;
   		$data['type'] = 2;
   		
   		$info['friend_id'] = $_POST['user_id'];//当前登录账号id(发送消息者)
   		$info['user_id'] = $_POST['uid'];//活动发布者id（接收信息者）
   		$info['type'] = 2;
   		
   		$count = Db::table('tp_apply')->where($info)->count();
   		if(!$count){
   			$res = Db::table('tp_apply')->insert($data);
   			if($res){
   				echo "验证消息发送成功！";
   			}else{
   				echo "验证消息发送失败！";
   			}
   		}else{
   			echo "加好友验证信息已发出，请等待验证结果！";
   		}
   	}
    public function other_message(){
    	$user_id = $_GET['user_id'];//当前登录者id
    	$uid = $_GET['uid'];//活动发布者id
    	
    	$user_info = Db::table("tp_user")->where(array("id"=>$uid))->find();
    	
    	$is_gz = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$uid,'isdelete'=>0))->count();//1已关注，0未关注

    	$info = Db::table("tp_info")
				->where(array("isdelete"=>0,'user_id'=>$uid))
				->order('create_time desc')
				->find();
				
		$this->view->assign('is_gz',$is_gz);
		$this->view->assign('user_info',$user_info);
		$this->view->assign('uid',$uid);
		$this->view->assign('user_id',$user_id);
		$this->view->assign('info',$info);
    	return $this->view->fetch();
    }
    /*关注*/
   public function friendgz(){
   		$user_id = $_POST['user_id'];
   		$friend_id = $_POST['friend_id'];

		$res = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$friend_id))->count();
		
		if($res == 0){
			$data['user_id'] = $_POST['user_id'];
	    	$data['friend_id'] = $_POST['friend_id'];
			$res = Db::table('tp_friendgz')->insert($data);
			if($res){
				echo "关注成功";
			}else{
				echo "关注失败";
			}
		}else{
			$result = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$friend_id,'isdelete'=>1))->count();
			if($result == 1){
				$data = array('isdelete'=>0);
   				$res = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$friend_id,'isdelete'=>1))->update($data);
   				if($res){
					echo "关注成功";
				}else{
					echo "关注失败";
				}
			}else{
				$data['user_id'] = $_POST['user_id'];
		    	$data['friend_id'] = $_POST['friend_id'];
				Db::table('tp_friendgz')->insert($data);
			}
		}
   }
   /*关注好友设置当前网址至session*/
   public function set_other_gz(){
   		Session::set('url','/index/other/other');
   		return 1;
   }
   
   /*取消关注*/
   public function del_gz(){
   		$user_id = $_POST['user_id'];
   		$friend_id = $_POST['uid'];
   		
   		$data = array('isdelete'=>1);
   		$res = Db::table('tp_friendgz')->where(array('user_id'=>$user_id,'friend_id'=>$friend_id,'isdelete'=>0))->update($data);
   		if($res){
   			echo "取消关注成功";
   		}else{
   			echo "取消关注失败";
   		}
   }
   /*发送消息*/
   public function message(){
   		$timer = time();
   		$data['content'] = $_POST['content'];
   		$data['user_id'] = $_POST['user_id'];//当前登录账号id(发送消息者)
   		$data['friend_id'] = $_POST['uid'];//活动发布者id（接收信息者）
   		$data['create_time'] = $timer;
   		
   		$res = Db::table('tp_chat')->insert($data);
   		if($res){
   			echo "消息发送成功！";
   		}else{
   			echo "消息发送失败！";
   		}
   }
   
   
}
